package cn.hinglo.gameclaw.client.action;

import cn.hinglo.gameclaw.client.service.UserInfoClientService;
import cn.hinglo.gameclaw.data.controller.BaseController;
import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.user.bean.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @createTime: 2018/1/9
 * @author: HingLo
 * @description: 用户信息请求管理
 */
@RestController
@RequestMapping("client/uic/")
public class UserInfoClientAction extends BaseController {
    private final UserInfoClientService userInfoClientService;

    @Autowired
    public UserInfoClientAction(UserInfoClientService userInfoClientService) {
        this.userInfoClientService = userInfoClientService;
    }


    /**
     * 登录成功后,获取信息
     *
     * @return 返回用户信息
     */
    @GetMapping("list")
    public Result userInfo() {
        String userId = getUserId(request);
        return success(userInfoClientService.userInfoService(userId));
    }

    /**
     * 单独获取用户的邀请码
     *
     * @return
     */
    @GetMapping("code")
    public Result code() {
        String userId = getUserId(request);
        UserInfo userInfo = userInfoClientService.userInfoService(userId);

        Map<String, Object> map = new HashMap<>(2);
        map.put("name", userInfo.getNikeName());
        map.put("code", userInfo.getUid());
        return success(map);
    }


    /**
     * 修改账号的信息，
     *
     * @param type  修改类型(nikeName,sex)
     * @param value 修改后的值
     * @return 操作结果
     */
    @PutMapping("update")
    public Result updateUserInfo(@RequestParam("type") int type, @RequestParam("value") String value) {
        String userId = getUserId(request);
        return userInfoClientService.updateUserInfoService(userId, type, value);
    }

    /**
     * 修改头像
     *
     * @param header 上出的为头像对象
     * @return 操作结果
     */
    @PostMapping("update/header")
    public Result updateUserInfoHeader(@RequestParam("header") MultipartFile header) {
        return userInfoClientService.updateUserInfoHeaderService(getUserId(request), header);
    }


}
